package com.meelive.ingkee.push.platform.vivo;

import android.content.Context;
import android.util.Log;

import com.meelive.ingkee.push.InkePushConfig;
import com.meelive.ingkee.push.InkePushHelper;
import com.meelive.ingkee.push.model.Constance;
import com.meelive.ingkee.push.model.InkePushType;
import com.meelive.ingkee.push.common.PushUtils;
import com.meelive.ingkee.push.platform.IPush;
import com.vivo.push.IPushActionListener;
import com.vivo.push.PushClient;
import com.vivo.push.util.VivoPushException;

import static com.meelive.ingkee.push.model.Constance.TAG;

/**
 * Create by: fanfeiyue
 * on: 2019-08-22
 * desc:
 * 集成指南 ：https://dev.vivo.com.cn/documentCenter/doc/233
 * api文档：https://swsdl.vivo.com.cn/appstore/developer/uploadfile/20190613/86430r/PUSH-UPS-API%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3%20-%202.4.4%E7%89%88.pdf
 */
public class InkeVivoPush extends IPush {
    @Override
    public InkePushType getPushType() {
        return InkePushType.VIVOPUSH;
    }

    @Override
    public void onRegister(final Context context, InkePushConfig config) {
        if (PushUtils.DEBUG) {
            Log.d(TAG, "InkeVivoPush onRegister");
        }
        boolean support = PushClient.getInstance(context).isSupport();
        if (!support) {
            InkePushHelper.getInstance().onErrorResponse(getPushType(), Constance.PUSH_NOT_SUPPORT, Constance.PUSH_NOT_SUPPORT_MSG);
            return;
        }

        try {
            PushClient.getInstance(context).initialize();
            PushClient.getInstance(context).turnOnPush(new IPushActionListener() {
                @Override
                public void onStateChanged(int state) {
                    if (state == 0) {
                        final String regId = PushClient.getInstance(context).getRegId();
                        InkePushHelper.getInstance().onReceiveToken(getPushType(), regId);
                    } else {
                        InkePushHelper.getInstance().onErrorResponse(getPushType(), state, "");
                    }
                }
            });
        } catch (VivoPushException e) {
            InkePushHelper.getInstance().onErrorResponse(InkePushType.VIVOPUSH, e.getCode(), e.getMessage());
        }
    }

    @Override
    public void onUnregister(Context context, InkePushConfig config) {
        PushClient.getInstance(context).turnOffPush(new IPushActionListener() {
            @Override
            public void onStateChanged(int i) {
                if (i == 0) {
                    Log.i("InkeVivoPush", "unregister token success");
                }
            }
        });
    }

}
